package com.mygdx.game.own;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;

public class Settings {

    private static final String file = ".kljump";
    //启用音乐
    public static boolean soundEnabled = true;
    //高分榜
    private static final int[] highscores = {100, 30, 20, 10, 5, 1};

    public static void load() {
        try {
            FileHandle fileHandle = Gdx.files.external(file);
            String[] strings = fileHandle.readString().split("\n");
            if (strings != null && strings.length > 1) {
                soundEnabled = Boolean.parseBoolean(strings[0]);
                for (int i = 0; i < 5; i++) {
                    highscores[i] = Integer.parseInt(strings[i + 1]);
                }
            }
        } catch (Throwable ex) {
            Gdx.app.error(Settings.class.getSimpleName(), ex.getMessage(), ex);
        }
    }

    public static void save() {
        try {
            FileHandle filehandle = Gdx.files.external(file);
            filehandle.writeString(Boolean.toString(soundEnabled) + "\n", false);
            for (int i = 0; i < 5; i++) {
                filehandle.writeString(Integer.toString(highscores[i]) + "\n", true);
            }
        } catch (Throwable e) {
        }
    }

    public static void addScore(int score) {
        for (int i = 0; i < 5; i++) {
            if (highscores[i] < score) {
                for (int j = 4; j > i; j--)
                    highscores[j] = highscores[j - 1];
                highscores[i] = score;
                break;
            }
        }
    }
}
